package com.home.andromo;


import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.widget.Toast;

public class Utilities {

	/*
	 * Get all installed application on mobile and return a list
	 * @param	c	Context of application
	 * @return	list of installed applications
	 */
	public static List getInstalledApplication(Context c, ArrayList<String> appsSeleccionadas) {
		List <ApplicationInfo> listaApps =c.getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA);
		ArrayList <Application> listaAppsOK = new ArrayList<Application>();
		
		if(appsSeleccionadas == null){
			for (ApplicationInfo application : listaApps) {
				if((application.flags & ApplicationInfo.FLAG_SYSTEM)!=1)
		          {
				listaAppsOK.add(new Application(application));
		          }
				
			}
			
		}else{
		for (ApplicationInfo app : listaApps) {
			if(appsSeleccionadas.contains(app.packageName)){
				if((app.flags & ApplicationInfo.FLAG_SYSTEM)!=1)
		          {
				listaAppsOK.add(new Application(app));
		          }
			}
		}
		
		
		}
		
		
		return listaAppsOK;
		
	}

	/*
	 * Launch an application
	 * @param	c	Context of application
	 * @param	pm	the related package manager of the context
	 * @param	pkgName	Name of the package to run
	 */
	public static boolean launchApp(Context c, PackageManager pm, String pkgName) {
		// query the intent for lauching
		Intent intent = pm.getLaunchIntentForPackage(pkgName);
		// if intent is available
		if(intent != null) {
			try {
				// launch application
				c.startActivity(intent);
				// if succeed
				return true;

			// if fail
			} catch(ActivityNotFoundException ex) {
				// quick message notification
				Toast toast = Toast.makeText(c, "Application Not Found", Toast.LENGTH_LONG);
				// display message
				toast.show();
			}
		}
		// by default, fail to launch
		return false;
	}
}
